!-------------------------------------------------------------LICENSE--------------------------------------------------------------!
!                                                                                                                                  !
!The MAP code is written in Fortran language for magnetohydrodynamics (MHD) calculation with the adaptive mesh refinement (AMR)    !
!and Message Passing Interface (MPI) parallelization.                                                                              !
!                                                                                                                                  !
!Copyright (C) 2012                                                                                                                !
!Ronglin Jiang                                                                                                                     !
!rljiang@ssc.net.cn                                                                                                                !
!585 Guoshoujing Road. Pudong, Shanghai, P.R.C. 201203                                                                             !
!                                                                                                                                  !
!This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License         !
!as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version.             !
!                                                                                                                                  !
!This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of    !
!MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more details.                        !
!                                                                                                                                  !
!You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software     !
!Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.                                                   !
!                                                                                                                                  !
!-------------------------------------------------------------LICENSE--------------------------------------------------------------!

!==================================================================================================================================|
subroutine model_variables (ro, mx, my, mz, bx, by, bz, en, gx, gy, gz, po, x, y, z, nx, ny, nz)
!==================================================================================================================================|

   use parameters
   implicit none

   integer(4), intent(in) :: nx, ny, nz

   real(8), intent(in) :: x(nx), y(ny), z(nz)
   real(8), dimension(nx, ny, nz), intent(inout) :: ro, mx, my, mz, bx, by, bz, en, gx, gy, gz, po

   real(8), dimension(nx, ny, nz) :: pr, te
   real(8), dimension(nz_total_finest) :: te_total_z

   integer(4) :: zeroz, k_total

   real(8) :: g0, te_chr, te_cor, h_cor, w_tr, dz, beta_1, beta_2, e_dir, r_x, r_y, r_z, ri, r, mp0

!----------------------------------------------------------------------------------------------------------------------------------|
!  1d te, ro distribution
!----------------------------------------------------------------------------------------------------------------------------------|
   g0 = -1.0d0
   te_chr = 0.6d0
   te_cor = 100.0d0
   h_cor = 8.5d0
   w_tr = 1.0d0

   if (.not. allocated (z_total)) allocate (z_total(nz_total_finest))
   if (.not. allocated (ro_total_z)) allocate (ro_total_z(nz_total_finest))

   if (ro_total_z_flag) then

      zeroz = (nint((nz_total - 2 * ng) * zero_posz / rangez) + ng) * 2 ** (n_levels - 1) + 1
      dz = rangez / dble(nz_total - 2 * ng) / 2.0d0 ** (n_levels - 1)

      z_total(zeroz) = dz * 0.5d0

      do k = zeroz + 1, nz_total_finest
         z_total(k) = z_total(k - 1) + dz
      enddo
      do k = zeroz - 1, 1, -1
         z_total(k) = z_total(k + 1) - dz
      enddo

      do k = 1, nz_total_finest
         te_total_z(k) = te_chr + (te_cor - te_chr) / 2.0d0 * (tanh((z_total(k) - h_cor) / w_tr) + 1.0d0 + eps)
      enddo

      ro_total_z(zeroz) = (2.0d0 * (te_total_z(zeroz - 1) + te_total_z(zeroz)) / 2.0d0 + dz * 0.5d0 * g0) /                        &
         (2.0d0 * te_total_z(zeroz) - dz * 0.5d0 * g0) * 1.0d3
      do k = zeroz + 1, nz_total_finest
         ro_total_z(k) = (2.0d0 * te_total_z(k - 1) + (z_total(k) - z_total(k - 1)) * g0) /                                        &
            (2.0d0 * te_total_z(k) - (z_total(k) - z_total(k - 1)) * g0) * ro_total_z(k - 1)
      enddo

      do k = zeroz - 1, 1, -1
         ro_total_z(k) = (2.0d0 * te_total_z(k + 1) - (z_total(k + 1) - z_total(k)) * g0) /                                        &
            (2.0d0 * te_total_z(k) + (z_total(k + 1) - z_total(k)) * g0) * ro_total_z(k + 1)
      enddo

      ro_total_z_flag = .false.

   endif

!----------------------------------------------------------------------------------------------------------------------------------|
!  initial value
!----------------------------------------------------------------------------------------------------------------------------------|
   e_dir = 1
   k_total = 1
   r = 5.0d0
   r_x = 0.0d0
   r_y = 0.0d0
   r_z = 80.0d0
   beta_1 = 0.2d0
   beta_2 = 0.5d0

   do k = 1, nz_total_finest
      if (abs(z_total(k) - 6.0d0) .le. 6.0d0) then
         mp0 = te_total_z(k) * ro_total_z(k) / (1.0d0 + beta_2)
      endif
   enddo

   do k = 1, nz
   do j = 1, ny
   do i = 1, nx

      gx(i, j, k) = 0.0d0
      gy(i, j, k) = 0.0d0
      gz(i, j, k) = g0

      te(i, j, k) = te_chr + (te_cor - te_chr) / 2.0d0 * (tanh ((z(k) - h_cor) / w_tr) + 1.0d0 + eps)

      do while (z_total(k_total) .lt. z(k))
         k_total = k_total + 1
      enddo

      if (k_total .eq. 1) then
         ro(i, j, k) = ro_total_z(k_total)
      else
         ro(i, j, k) = (ro_total_z(k_total - 1) * (z_total(k_total) - z(k)) +                                                      &
                        ro_total_z(k_total) * (z(k) - z_total(k_total - 1))) /                                                     &
                       (z_total(k_total) - z_total(k_total - 1))
      endif
      pr(i, j, k) = ro(i, j, k) * te(i, j, k)
      mx(i, j, k) = 0.0d0
      my(i, j, k) = 0.0d0
      mz(i, j, k) = 0.0d0
      bx(i, j, k) = 0.0d0
      by(i, j, k) = 0.0d0
      bz(i, j, k) = 0.0d0
      ri = sqrt((x(i) - r_x) ** 2 + (y(j) - r_y) ** 2 + (z(k) - r_z) ** 2)
      if (ri .le. r) then
         ro(i, j, k) = ro(i, j, k) * 100.0d0
         bx(i, j, k) = -sqrt(pr(i, j, k) / (1.0d0 + beta_1) * 2.0d0) * e_dir * (y(j) - r_y) / (ri + eps)
         by(i, j, k) =  sqrt(pr(i, j, k) / (1.0d0 + beta_1) * 2.0d0) * e_dir * (x(i) - r_x) / (ri + eps)
         pr(i, j, k) = pr(i, j, k) * beta_1 / (1.0d0 + beta_1)
         if (pr(i, j, k) .le. 0.0d0) then 
            write(*, *) 'ERROR: abnormal gas pressure (position 1).'
            write(*, *) 'pr = ', pr(i, j, k)
            call mpi_abort(mpi_comm_world, 1, error)
         endif
      endif

      if (abs(z(k) - 5.0d0) .le. 5.0d0) then
         by(i, j, k) = sqrt(mp0 * 2.0d0)
         pr(i, j, k) = pr(i, j, k) - mp0
         if (pr(i, j, k) .le. 0.0d0) then 
            write(*, *) 'ERROR: abnormal gas pressure (position 2).'
            write(*, *) 'pr = ', pr(i, j, k)
            call mpi_abort(mpi_comm_world, 1, error)
         endif
      endif

      en(i, j, k) = (mx(i, j, k) * mx(i, j, k) + my(i, j, k) * my(i, j, k) + mz(i, j, k) * mz(i, j, k)) / ro(i, j, k) / 2.0d0 +    &
         (bx(i, j, k) * bx(i, j, k) + by(i, j, k) * by(i, j, k) + bz(i, j, k) * bz(i, j, k)) / 2.0d0 + pr(i, j, k) / gmm1

      po(i, j, k) = 0.0d0
   enddo
   enddo
   enddo

!----------------------------------------------------------------------------------------------------------------------------------|
end subroutine model_variables
